package org.whz.service.impl;

import org.whz.entity.Commodity;
import org.whz.entity.Coupon;
import org.whz.entity.Detail;
import org.whz.service.SellService;

import java.math.BigDecimal;
import java.util.List;

public class SellServiceImpl implements SellService {
    @Override
    public BigDecimal collectAmount(List<Detail> detailList) throws Exception {
        if(detailList == null || detailList.isEmpty()){
            throw  new Exception("没有收入款项");
        }
        //商品总价合计
        BigDecimal commodityAmountSum = new BigDecimal("0.00");
        for (Detail  detail : detailList){
            Commodity commodity =  detail.getCommodity();
            /**
             * sum += 价格 * 重量 * 优惠折扣
             */
            BigDecimal commodityAmount =  (commodity.getPrice().multiply(new BigDecimal(detail.getWeight())).multiply(commodity.getDiscount()));
            commodityAmountSum = commodityAmountSum.add(commodityAmount);
        }
        BigDecimal actuallyAmount = commodityAmountSum.setScale(2,BigDecimal.ROUND_DOWN);
        return  actuallyAmount;
    }

    public  BigDecimal  collectAmount(List<Detail> detailList, List<Coupon> couponList) throws Exception {
        BigDecimal commodityAmountSum = this.collectAmount(detailList);
        //消费劵合计
        BigDecimal couponAmountSum = new BigDecimal("0.00");
        if(couponList != null && !couponList.isEmpty()){
            for (Coupon coupon : couponList){
                //商品总金额大于等于 消费劵条件金额 可以使用消费劵
                if(commodityAmountSum.compareTo(coupon.getConditionAmount()) >= 0){
                    couponAmountSum = couponAmountSum.add(coupon.getAmount());
                }
            }
        }
        //实付金额
        BigDecimal actuallyAmount = commodityAmountSum.subtract(couponAmountSum).setScale(2,BigDecimal.ROUND_DOWN);
        return  actuallyAmount;
    }
}
